﻿using MicroMvvm;
using ModernUILogViewer.ViewModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace ModernUILogViewer.Settings
{
    class SettingsManager
    {
        //public ICommand SaveColorSettings { get { return new RelayCommand(SaveColorSettingsExecute); } }
        //public ICommand ReloadColorSettings { get { return new RelayCommand(ReloadColorSettingsExecuted); } }
        public ICommand SaveRegexSettings { get { return new MvvmFoundation.Wpf.RelayCommand<OptionsViewModel>(SaveRegexSettingsExecute); } }
        public ICommand ReloadRegexSettings { get { return new MvvmFoundation.Wpf.RelayCommand<OptionsViewModel>(LoadRegexSettingsExecute); } }
        public ICommand SaveNetworkSettings { get { return new MvvmFoundation.Wpf.RelayCommand<NetworkViewModel>(SaveNetworkSettingsExecute); } }
        public ICommand SaveFileWatcherSettings { get { return new MvvmFoundation.Wpf.RelayCommand<FileWatcherOptionsViewModel>(SaveFileWatcherSettingsExecute); } }
        public ICommand ReloadFileWatcherSettings { get { return new MvvmFoundation.Wpf.RelayCommand<FileWatcherOptionsViewModel>(LoadFileWatcherSettingsExecute); } }        
        public ICommand ReloadNetworkSettings { get { return new MvvmFoundation.Wpf.RelayCommand<NetworkViewModel>(LoadNetworkSettingsExecute); } }
        public ICommand SaveExtEditorSettings { get { return new MvvmFoundation.Wpf.RelayCommand<ExternalEditorViewModel>(SaveExtEditorExecute); } }
        public ICommand ReloadExtEditorSettings { get { return new MvvmFoundation.Wpf.RelayCommand<ExternalEditorViewModel>(LoadExtEditorExecute); } }
        public ICommand LoadInitialSettings { get { return new MvvmFoundation.Wpf.RelayCommand<MainViewModel>(LoadInitialSettingsExecute); } }
        public ICommand SaveAppSettings { get { return new MvvmFoundation.Wpf.RelayCommand<MainViewModel>(SaveAppSettingsExecute); } }
        public ICommand ReloadSettings { get { return new MvvmFoundation.Wpf.RelayCommand<ApplicationSettingsBase>(ReloadSettingsExecute); } }
        public ICommand SaveSettings { get { return new MvvmFoundation.Wpf.RelayCommand<ApplicationSettingsBase>(SaveSettingsExecute); } }        

        public event EventHandler SettingsSaved;
        public event EventHandler SettingsReloaded;

        void SaveFileWatcherSettingsExecute(FileWatcherOptionsViewModel fileWatcherOptionsVM)
        {
            FileWatcherSettings settings = (FileWatcherSettings)App.Current.FindResource("fileWatcherOptionsVM");

            settings.EnableCredentials = fileWatcherOptionsVM.EnableCredentials;
            settings.EnableProxy = fileWatcherOptionsVM.EnableProxy;
            settings.InitialLimit = fileWatcherOptionsVM.InitialLimit;
            settings.Host = fileWatcherOptionsVM.Host;
            settings.Port = fileWatcherOptionsVM.Port;
            settings.User = fileWatcherOptionsVM.User;
            settings.Password = fileWatcherOptionsVM.Password;
            settings.Domain = fileWatcherOptionsVM.Domain;

            settings.Save();

            RaiseEventInvoker(SettingsSaved);
        }

        void LoadFileWatcherSettingsExecute(FileWatcherOptionsViewModel fileWatcherOptionsVM)
        {
            FileWatcherSettings settings = (FileWatcherSettings)App.Current.FindResource("fileWatcherOptionsVM");

            fileWatcherOptionsVM.EnableCredentials = settings.EnableCredentials;
            fileWatcherOptionsVM.EnableProxy = settings.EnableProxy;
            fileWatcherOptionsVM.InitialLimit = settings.InitialLimit;
            fileWatcherOptionsVM.Host = settings.Host;
            fileWatcherOptionsVM.Port = settings.Port;
            fileWatcherOptionsVM.User = settings.User;
            fileWatcherOptionsVM.Password = settings.Password;
            fileWatcherOptionsVM.Domain = settings.Domain;

            RaiseEventInvoker(SettingsReloaded);
        }


        void SaveExtEditorExecute(ExternalEditorViewModel exteditVM)
        {
            ExtEditorSettings settings = (ExtEditorSettings)App.Current.FindResource("exteditsettings");

            settings.ExePath = exteditVM.ExePath;
            settings.CommandArgs = exteditVM.CommandArgs;

            settings.Save();

            RaiseEventInvoker(SettingsSaved);
        }

        void LoadExtEditorExecute(ExternalEditorViewModel exteditVM)
        {
            ExtEditorSettings settings = (ExtEditorSettings)App.Current.FindResource("exteditsettings");

            exteditVM.ExePath = settings.ExePath;
            exteditVM.CommandArgs = settings.CommandArgs;

            RaiseEventInvoker(SettingsReloaded);
        }

        private static void SaveAppSettingsExecute(MainViewModel mainVM)
        {
            AppSettings settings = (AppSettings)App.Current.FindResource("settings");

            HomeViewModel homeVM = mainVM.HomeVM;

            settings.MatchWholeWord = homeVM.MatchWholeWord;
            settings.MatchIgnoreCase = homeVM.MatchIgnoreCase;

            SettingsAppearanceViewModel settingsAppearanceVM = (SettingsAppearanceViewModel)App.Current.FindResource("settingsAppearanceVM");

            settings.SelectedAccentColor = settingsAppearanceVM.SelectedAccentColor;
            settings.SelectedThemeIndex = settingsAppearanceVM.Themes.IndexOf(settingsAppearanceVM.SelectedTheme);
            settings.SelectedFontSize = settingsAppearanceVM.SelectedFontSize;

            settings.Save();
        }

        private void LoadNetworkSettingsExecute(NetworkViewModel networkVM)
        {
            NetworkSettings settings = (NetworkSettings)App.Current.FindResource("networksettings");

            networkVM.EnableProxy = settings.EnableProxy;
            networkVM.Host = settings.Host;
            networkVM.Port = settings.Port;
            networkVM.User = settings.User;
            networkVM.Password = settings.Password;
            networkVM.Domain = settings.Domain;

            RaiseEventInvoker(SettingsReloaded);
        }

        private void SaveNetworkSettingsExecute(NetworkViewModel networkVM)
        {
            NetworkSettings settings = (NetworkSettings)App.Current.FindResource("networksettings");

            settings.EnableProxy = networkVM.EnableProxy;
            settings.Host = networkVM.Host;
            settings.Port = networkVM.Port;
            settings.User = networkVM.User;
            settings.Password = networkVM.Password;
            settings.Domain = networkVM.Domain;

            settings.Save();

            RaiseEventInvoker(SettingsSaved);
        }

        private void SaveRegexSettingsExecute(OptionsViewModel optionsVM)
        {
            RegexSettings regexSettings = (RegexSettings)App.Current.FindResource("regexsettings");

            regexSettings.DateFormat = optionsVM.DateFormat;
            regexSettings.DateGroupMatch = optionsVM.DateGroupMatch;
            regexSettings.DateRegExp = optionsVM.DateRegExp;
            regexSettings.LevelGroupMatch = optionsVM.LevelGroupMatch;
            regexSettings.LevelRegExp = optionsVM.LevelRegExp;
            regexSettings.LineBeginningRegExp = optionsVM.LineBeginningRegExp;
            regexSettings.IncludeDateMatch = optionsVM.IncludeDateMatch;
            regexSettings.IncludeLevelMatch = optionsVM.IncludeLevelMatch;
            regexSettings.SelectedCultureInfo = optionsVM.SelectedCultureInfo;

            regexSettings.Save();

            RaiseEventInvoker(SettingsSaved);
        }

        private void LoadRegexSettingsExecute(OptionsViewModel optionsVM)
        {
            RegexSettings regexSettings = (RegexSettings)App.Current.FindResource("regexsettings");

            optionsVM.DateFormat = regexSettings.DateFormat;
            optionsVM.DateGroupMatch = regexSettings.DateGroupMatch;
            optionsVM.DateRegExp = regexSettings.DateRegExp;
            optionsVM.LevelGroupMatch = regexSettings.LevelGroupMatch;
            optionsVM.LevelRegExp = regexSettings.LevelRegExp;
            optionsVM.LineBeginningRegExp = regexSettings.LineBeginningRegExp;
            optionsVM.IncludeDateMatch = regexSettings.IncludeDateMatch;
            optionsVM.IncludeLevelMatch = regexSettings.IncludeLevelMatch;
            optionsVM.SelectedCultureInfo = regexSettings.SelectedCultureInfo;

            RaiseEventInvoker(SettingsReloaded);
        }

        private void SaveSettingsExecute(ApplicationSettingsBase settings)
        {
            settings.Save();

            RaiseEventInvoker(SettingsSaved);
        }

        private void ReloadSettingsExecute(ApplicationSettingsBase settings)
        {
            settings.Reload();

            RaiseEventInvoker(SettingsReloaded);
        }

        protected void RaiseEventInvoker(EventHandler eve)
        {
            if (App.Current.Dispatcher.CheckAccess())
            {
                if (eve != null)
                    eve.Invoke(this, EventArgs.Empty);
            }
            else
            {
                App.Current.Dispatcher.Invoke((Action)delegate()
                {
                    if (eve != null)
                        eve.Invoke(this, EventArgs.Empty);
                });
            }
        }

        private void LoadInitialSettingsExecute(MainViewModel mainVM)
        {
            AppSettings settings = (AppSettings)App.Current.FindResource("settings");

            HomeViewModel homeVM = mainVM.HomeVM;

            homeVM.MatchWholeWord = settings.MatchWholeWord;
            homeVM.MatchIgnoreCase = settings.MatchIgnoreCase;

            SettingsAppearanceViewModel settingsAppearanceVM = (SettingsAppearanceViewModel)App.Current.FindResource("settingsAppearanceVM");

            //SettingsAppearanceViewModel settingsVM = mainVM.SettingsVM;

            settingsAppearanceVM.SelectedAccentColor = settings.SelectedAccentColor;
            settingsAppearanceVM.SelectedTheme = settingsAppearanceVM.Themes[settings.SelectedThemeIndex];
            settingsAppearanceVM.SelectedFontSize = settings.SelectedFontSize;

            LoadRegexSettingsExecute(mainVM.OptionsVM);
            LoadNetworkSettingsExecute(mainVM.NetworkVM);
            LoadFileWatcherSettingsExecute(mainVM.FileWatcherOptionsVM);
            LoadExtEditorExecute(mainVM.ExternalEditorVM);
        }

        
    }
}
